/**
 * RemoteClientObserver.java
 */
package fss.remote;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Observable;
import java.util.Observer;

import fss.util.rmi.RmiId;

/**
 * This is a wrapper for all client objects so that they can be registered
 * with Java Observable/Observer framework.
 * 
 * @author thinh
 *
 */
public class RemoteClientObserver extends UnicastRemoteObject implements Observer {
    private static final long serialVersionUID = 1L;
    /**
     * The wrapped remote client.
     */
    private RemoteClient wrapped = null;
    /**
     * Just in case the wrapped remote object is dead.
     */
    private RmiId id = null;
    /**
     * Constructor wrapping a remote client.
     * @param client the remote client.
     * @throws RemoteException error communicating with remote client.
     */
    public RemoteClientObserver(RemoteClient client) throws RemoteException {
        super();
        wrapped = client;
        id = wrapped.getId();
    }
    /**
     * Get the wrapped remote client.
     * @return the remote client.
     */
    public RemoteClient getRemoteClient() {
        return wrapped;
    }
    /**
     * Get the wrapped id.
     * @return the id.
     */
    public RmiId getId() {
        return id;
    }
    /**
     * Return whether or not the remote service is available..
     * @return true if available; false otherwise.
     */
    public boolean available() {
        boolean available = true;
        try {
            wrapped.getId();
        } catch (RemoteException e) {
            available = false;
        }
        return available;
    }
    @Override
    public void update(Observable observable, Object message) {
        try {
            wrapped.update(observable, message);
        } catch (RemoteException e) {
            observable.deleteObserver(this);
        }
    }    
}
